package com.qf.flash.ui.chat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.ui.EaseBaseActivity;
import com.hyphenate.easeui.ui.EaseChatFragment;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.hyphenate.easeui.widget.chatrow.EaseCustomChatRowProvider;
import com.qf.dblib.dao.entity.UserInfo;
import com.qf.dblib.dao.impl.UserMgr;
import com.qf.flash.R;
import com.qf.flash.constant.Constant;
import com.qf.flash.constant.Constants;
import com.qf.flash.constant.FlashCodes;
import com.qf.flash.ui.chat.rowProvider.MyEaseChatProvider;
import com.qf.flash.ui.login.HomeActivity;
import com.qf.flashlib.app.FlashLib;
import com.qf.flashlib.permission.PermissionManager;

import java.util.List;

/**
 * 聊天页面（对话窗口）
 */
public class ChatActivity extends EaseBaseActivity implements EaseChatFragment.EaseChatFragmentHelper {
    public static ChatActivity activityInstance;
    private MyChatFragment chatFragment;
    String toChatUsername;
    private EaseTitleBar titleBar;
    private String type;
    private String showName;

    public static void start(Activity activity, String toChatUsername) {
        Intent intent = new Intent(activity, ChatActivity.class);
        intent.putExtra(EaseConstant.EXTRA_USER_ID, toChatUsername);
        activity.startActivity(intent);
    }


    public static void goGroupChat(Context context, int type, String emchatId) {
        Intent intent = new Intent(context, ChatActivity.class);
        // it is group chat
        intent.putExtra("chatType", type);
        intent.putExtra("userId", emchatId);

        context.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_chat);
        activityInstance = this;
        //user or group id
        toChatUsername = getIntent().getStringExtra(EaseConstant.EXTRA_USER_ID);
        type = getIntent().getStringExtra(Constant.EXTRA_CHAT_TYPE);
        showName = getIntent().getStringExtra("showName");
        chatFragment = new MyChatFragment();
        //set arguments

        chatFragment.setArguments(getIntent().getExtras());
        chatFragment.setChatFragmentHelper(this);
        getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();

//        titleBar = chatFragment.getView().findViewById(R.id.title_bar);
//        titleBar.setRightImageResource(R.mipmap.nav_more);
        checkPermission();
    }

    private void checkPermission() {
        new PermissionManager(this)
                .addPermission(Constants.TAKE_PHOTO_PERMISSION)
                .addListener(new PermissionManager.OnPermissionListener() {
                    @Override
                    public void onSuccess(List<String> permissions) {
                    }

                    @Override
                    public void onFailed(List<String> permissions) {
                        FlashLib.showToast("权限拒绝");
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        activityInstance = null;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        // enter to chat activity when click notification bar, here make sure only one chat activiy
        String username = intent.getStringExtra("userId");
        if (toChatUsername.equals(username))
        { super.onNewIntent(intent);}
        else {
            finish();
            startActivity(intent);
        }

    }

    @Override
    public void onBackPressed() {
        chatFragment.onBackPressed();
    }

    public String getToChatUsername() {
        return toChatUsername;
    }

    @Override
    public void onSetMessageAttributes(EMMessage message) {
        setUserInfoAttribute(message);
    }

    @Override
    public void onEnterToChatDetails() {

    }

    @Override
    public void onAvatarClick(String username) {

    }

    @Override
    public void onAvatarLongClick(String username) {

    }

    @Override
    public boolean onMessageBubbleClick(EMMessage message) {
        return false;
    }

    @Override
    public void onMessageBubbleLongClick(EMMessage message) {

    }

    @Override
    public boolean onExtendMenuItemClick(int itemId, View view) {
        return false;
    }

    @Override
    public EaseCustomChatRowProvider onSetCustomChatRowProvider() {
        return new MyEaseChatProvider();
    }


    /**
     * 设置用户的属性，
     * 通过消息的扩展，传递客服系统用户的属性信息
     * @param message
     */
    private void setUserInfoAttribute(EMMessage message) {
        try {
            UserInfo loginUser = UserMgr.getInstance().getLoginUser();

            message.setAttribute(FlashCodes.CHAT_UID, loginUser.getUid());
            message.setAttribute(FlashCodes.CHAT_NICK_NAME, loginUser.getName());
            message.setAttribute(FlashCodes.CHAT_PIC_AVATAR, loginUser.getPhoto()) ;//这里用是图片的完整链接地址，如果要取缩略图，需要服务端配合；
            message.setAttribute(FlashCodes.CHAT_PIC_FRAME, loginUser.getPhotoLineImage()) ;//这里用是图片的完整链接地址，如果要取缩略图，需要服务端配合；

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void backToHome(){
        HomeActivity.start(this,2);
    }
}
